package com.gitee.cashzhang27.test.cloud.oauth.resource.server.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/02/09 13:43
 */
public class LocalUserContext {


  private static ObjectMapper MAPPER = new ObjectMapper();

  public static String getUserId() {
    Object details = SecurityContextHolder.getContext()
        .getAuthentication().getDetails();
    HashMap<String, String> hashMap = new HashMap<>();
    try {
      String json = MAPPER.writeValueAsString(details);
      hashMap = MAPPER.readValue(json, HashMap.class);
      String decodedDetails = MAPPER.writeValueAsString(hashMap.get("decodedDetails"));
      hashMap = MAPPER.readValue(decodedDetails, HashMap.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return hashMap.get("user_id");
  }
}
